package by.qizer.supercamp.web;

import by.qizer.supercamp.domain.Owner;
import by.qizer.supercamp.service.OwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * @author Pribytkoyski Alexey
 *         User: qizer
 *         Date: 04.04.12
 */

@Controller
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @RequestMapping(value = "/owneradd", method = RequestMethod.POST)
    public String addOwner(@ModelAttribute("owner") Owner owner, BindingResult result) {
        ownerService.addOwner(owner);
        return "redirect:/owner";
    }

    @RequestMapping("/owner")
    public String listOwners(Map<String, Object> map) {
        map.put("owner", new Owner());
        map.put("ownerList", ownerService.listOwners());
        return "owner";
    }

    @RequestMapping("/ownerdel/{ownerId}")
    public String deleteOwner(@PathVariable("ownerId") Integer ownerId) {
        ownerService.removeOwner(ownerId);
        return "redirect:/owner";
    }
}
